function parseData(){
	var action=getQ().action
	if(action==="edit"){
		//
	}
	else if(action==="audit"){
		document.getElementById("done").style.display="none";
	}
	else{
		document.getElementById("done").style.display="none";
	}
	var s=getQ().s
	document.getElementById("name").textContent="作业名称："+getQ().name
	$.ajax({
		"url":"/ti/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"data":{"data":s,"action":action,"student":getQ().student,"ti_id":getQ().ti_id},
		"complete":stop_load_pic,
		"dataType":"jsonp",
		"success":function(data){
			if(!data.status){
				showErrorInfo(data.content)
				return false;
			}
			var data=data.content
			var p=document.getElementById("p")
			for(var i=0;i<data.length;i++){
				var tempa=document.getElementsByClassName("temp-a")[0].cloneNode(true)
				tempa.style.display="block"
				tempa.setAttribute("type",data[i].type)
				tempa.setAttribute("sid",data[i].id)
				$(tempa).find(".timu-title").text("题目"+(i+1)+"、")
				$(tempa).find(".timu-content").text(data[i].name+"("+ data[i].type+")")
				if(data[i].pic_title!==""){
					$(tempa).find(".img").css("display","block").find("img").attr("src",data[i].pic_title)
				}
				

				var answer=document.getElementsByClassName("temp-b")[0].cloneNode(true)
				answer.style.display="block"
				$(answer).find(".order").text("A")
				$(answer).find(".order-content").text(data[i].question_a)
				if(data[i].pic_a!==""){
					$(answer).find(".img").css("display","block").find("img").attr("src",data[i].pic_a)
				}
				tempa.appendChild(answer)

				var answer=document.getElementsByClassName("temp-b")[0].cloneNode(true)
				answer.style.display="block"
				$(answer).find(".order").text("B")
				$(answer).find(".order-content").text(data[i].question_b)
				if(data[i].pic_b!==""){
					$(answer).find(".img").css("display","block").find("img").attr("src",data[i].pic_b)
				}
				tempa.appendChild(answer)
				if (data[i].type!=="判断题"){
					var answer=document.getElementsByClassName("temp-b")[0].cloneNode(true)
					answer.style.display="block"
					$(answer).find(".order").text("C")
					$(answer).find(".order-content").text(data[i].question_c)
					if(data[i].pic_c!==""){
						$(answer).find(".img").css("display","block").find("img").attr("src",data[i].pic_c)
					}
					tempa.appendChild(answer)

					var answer=document.getElementsByClassName("temp-b")[0].cloneNode(true)
					answer.style.display="block"
					$(answer).find(".order").text("D")
					$(answer).find(".order-content").text(data[i].question_d)
					if(data[i].pic_d!==""){
						$(answer).find(".img").css("display","block").find("img").attr("src",data[i].pic_d)
					}
					tempa.appendChild(answer)
				}
				//渲染学生答题记录
				var reply=data[i].reply
				$(tempa).find("button").each(function(){
					for(var k=0;k<reply.length;k++){
						if (this.textContent.toUpperCase()===reply[k].toUpperCase()){
							$(this).addClass("btn-success")
						}
					}
					
				})
				//判断答案
				var answer=data[i].answer
				if(action==="audit"){
					console.log(data)
					if(answer.toString()==reply.toString() ){
						if (reply.length>0){
							//没有答题的情况下
							$(tempa).find(".judge").addClass("glyphicon-ok").css("display","inline").css("color","green")
							$(tempa).find(".timu-content").css("color","green")
						}
					}
					else{
						$(tempa).find(".timu-content").css({"border":"red 2px solid","color":"red"})
						$(tempa).find(".judge").addClass("glyphicon-remove").css("display","inline").css("color","red")
					}
					$(tempa).find(".temp-c").css("display","block").find(".text").text("正确答案："+data[i].answer_string)
				}	



				p.appendChild(tempa)
			}
		}
	})
	
}
$(document).on("click",".temp-a button",function(){
	var type=$(this).parent().parent().parent().attr("type")
	if( type==="多选题"){
		if($(this).hasClass("btn-success")){
			$(this).removeClass("btn-success")
		}
		else{
			$(this).addClass("btn-success")
		}
	}
	else if(type==="单选题" || type==="判断题"){
		console.log(123)
		if($(this).hasClass("btn-success")){
			$(this).removeClass("btn-success")
		}
		else{
			$(this).parent().parent().parent().find("button").removeClass("btn-success")
			$(this).addClass("btn-success")
		}
	}
})
function submitTi(){
	var data=[]
	$(".temp-a").each(function(){
		if(this.style.display==="block"){
			//去掉none不显示的
			var t={}
			t.inventory_id=this.getAttribute("sid")
			t.ti_id=getQ().ti_id
			t.reply=[]
			$(this).find(".btn-success").each(function(){
				// 有btn-success的就是填写的答案
				if(this.parentNode.parentNode.style.display!=="none"){
					t.reply.push(this.textContent)
				}
			})
			data.push(t)
		}
	})
	data=JSON.stringify(data)
	jQuery.ajax({
		"url":"/do_ti/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"data":{"data":data},
		"type":"POST",
		"success":function(data){
			data=JSON.parse(data)
			responseCheck(data)
			if(!data.status){
				showErrorInfo(data.content)
				return false;
			}
			alert("已提交！")
			window.close()
		}
	})
	
}
parseData()
document.getElementById("done").onclick=function(){
	submitTi();
}

